﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Data.Common;

namespace CommonUtils
{
    /// <summary>
    /// ADO.NET for Oracle
    /// </summary>
    public class DbOracle : DbBase
    {
        public static class ConnectStrings
        {
            public static string Get(string host, string database, string user, string pwd, int port = 1521)
            => string.Format("Data Source={0}:{4}/{1};User ID={2};Password={3};", host, database, user, pwd, port);

            //"User Id=admin;Password=123;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=test)))";

            public static string Get1(string host, string database, string user, string pwd, int port = 1521)
            => string.Format("User Id={2};Password={3};Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={4})))(CONNECT_DATA=(SERVICE_NAME={1})))", host, database, user, pwd, port);
        }

        public DbOracle(string connectString)
        : base(connectString) { }

        public DbOracle(string host, string database, string user, string pwd, int port = 1521)
        : this(ConnectStrings.Get(host, database, user, pwd, port)) { }

        protected override DbConnection GetConnection()
        => new OracleConnection(ConnectString);

        protected override DbParameter GetParameter(string key, object value)
        => new OracleParameter(key, value);

        [Obsolete]
        protected override DbDataAdapter GetDataAdapter(DbCommand cmd)
        => new OracleDataAdapter((OracleCommand)cmd);

        protected override char ParameterIdentifier { get; } = ':';
    }
}
